package studio.raptor.databus.command;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Set;

/**
 * Created  on 2017/4/25.
 */
public class Event implements Serializable {

    private static final long serialVersionUID = 9026242539300038104L;

    /**
     * 事件基础数据
     */
    private EventBasicData eventBasicData;
    /**
     * 事件业务数据
     */
    private EventBusinessData eventBusinessData;
    /**
     * 事件元信息
     */
    private EventMetaData eventMetaData;

    public EventBasicData getEventBasicData() {
        return eventBasicData;
    }

    public void setEventBasicData(EventBasicData eventBasicData) {
        this.eventBasicData = eventBasicData;
    }

    public EventBusinessData getEventBusinessData() {
        return eventBusinessData;
    }

    public void setEventBusinessData(EventBusinessData eventBusinessData) {
        this.eventBusinessData = eventBusinessData;
    }

    public EventMetaData getEventMetaData() {
        return eventMetaData;
    }

    public void setEventMetaData(EventMetaData eventMetaData) {
        this.eventMetaData = eventMetaData;
    }

    public Event(EventBasicData eventBasicData, EventBusinessData eventBusinessData, EventMetaData eventMetaData) {
        this.eventBasicData = eventBasicData;
        this.eventBusinessData = eventBusinessData;
        this.eventMetaData = eventMetaData;
    }

    private Event(){}

    /**
     * 校验event事件信息内容
     *
     * @return 返回值为空字符串说明校验通过
     */
    public String validate() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        //校验事件基础数据
        if (eventBasicData == null) {
            return "eventBasicData should not be null";
        }
        Set<ConstraintViolation<EventBasicData>> basicDataViolations = validator.validate(eventBasicData);
        for (ConstraintViolation<EventBasicData> constraintViolation : basicDataViolations) {
            return "eventBasicData:" + constraintViolation.getMessage();
        }
        //补充校验基础数据中投递事件2月份天数是否正确
        String deliverTime = eventBasicData.getDeliverTime();
        String date = deliverTime.split("[ ]")[0];
        int month = Integer.valueOf(date.split(EventBasicData.DATE_SEPARATOR)[1]);
        if (month == 2) {
            int year = Integer.valueOf(date.split(EventBasicData.DATE_SEPARATOR)[0]);
            Calendar cal = Calendar.getInstance();
            //计算得到2月份的最后一天
            cal.set(year, 1, 1);
            int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            if ( lastDay < Integer.valueOf(date.split(EventBasicData.DATE_SEPARATOR)[2]) ){
                return "eventBasicData:deliverTime["+deliverTime+"] is not legal" ;
            }

        }
        if (eventMetaData == null) {
            return "eventMetaData should not be null";
        }
        //校验事件元数据
        Set<ConstraintViolation<EventMetaData>> metaDataViolations = validator.validate(eventMetaData);
        for (ConstraintViolation<EventMetaData> constraintViolation : metaDataViolations) {
            return "eventMetaData:" + constraintViolation.getMessage();
        }
        //校验事件业务数据
        if (eventBusinessData == null) {
            return "eventBusinessData should not be null";
        }
        if (eventBusinessData.getProvideType().getValue() == EventBusinessDataProvideType.PROVIDE_TYPE_CARRY.getValue()) {
            //自携带数据方式
            String dataContent = eventBusinessData.getContent();
            if (dataContent == null || "".equals(dataContent)) {
                return "eventBusinessData:dataContent should not be empty ";
            }
        }
        //校验通过
        return "";
    }

    @Override
    public String toString() {
        return "Event{" +
            "eventBasicData=" + eventBasicData +
            ", eventBusinessData=" + eventBusinessData +
            ", eventMetaData=" + eventMetaData +
            '}';
    }
}
